<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动画效果</title>
    <style>
        div{
            position: absolute;
            left: 0;
            width: 100px;height: 100px;
            background-color:aqua;
        }
        span{
            position: absolute;
            top: 200px;
            left: 0;
            width: 150px;height: 150px;
            background-color:aquamarine;
        }
    </style>
</head>
<body>
    <div></div>
    <button>span</button>
    <span>span</span>
    <script>
        var div=document.querySelector('div');
        var span=document.querySelector('span');
        var button=document.querySelector('button');
        function animate(obj,size){
            clearInterval(obj.timer);
        obj.timer=setInterval(() => {
            if(obj.offsetLeft>=size){
                clearInterval(obj.timer);
            }
            obj.style.left=obj.offsetLeft + 3 +'px'
        }, 20);}
       animate(div,300);
       button.addEventListener('click',function(){
        animate(span,400);
       })
       
    </script>
</body>
</html>